/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.Pricelist;
import org.crystalrange.enterprise.service.PricelistService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class PricelistListController extends BaseController {

    protected PricelistService pricelistService;
    protected Pricelist selectedPricelist;
    protected Listbox listboxPricelist;
    protected Search searchPricelist;
    protected List<Pricelist> pricelists;

    public PricelistService getPricelistService() {
        return this.pricelistService;
    }

    public void setPricelistService(PricelistService pricelistService) {
        this.pricelistService = pricelistService;
    }

    public Pricelist getSelectedPricelist() {
        return this.selectedPricelist;
    }

    public void setSelectedPricelist(Pricelist selectedPricelist) {
        this.selectedPricelist = selectedPricelist;
    }

    public List<Pricelist> getPricelists() {
        return this.pricelists;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchPricelist = new Search(Pricelist.class);
        this.searchPricelist.setMaxResults(100);
        this.searchPricelist.addFetch("createdBy");
        this.searchPricelist.addFetch("updatedBy");
        this.pricelists = this.pricelistService.search(this.searchPricelist);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemPricelistCreate(Event event) throws Exception {
        this.pricelistCreate();
    }

    public void onClick$menuitemPricelistEdit(Event event) throws Exception {
        this.pricelistEdit();
    }

    public void onClick$menuitemPricelistDelete(Event event) throws Exception {
        this.pricelistDelete();
    }

    public void onClick$menuitemPricelistSearch(Event event) throws Exception {
        this.pricelistSearch();
    }

    public void onClick$menuitemPricelistRefresh(Event event) throws Exception {
        this.pricelistRefresh();
    }

    public void onClick$toolbarbuttonPricelistCreate(Event event) throws Exception {
        this.pricelistCreate();
    }

    public void onClick$toolbarbuttonPricelistEdit(Event event) throws Exception {
        this.pricelistEdit();
    }

    public void onClick$toolbarbuttonPricelistDelete(Event event) throws Exception {
        this.pricelistDelete();
    }

    public void onClick$toolbarbuttonPricelistSearch(Event event) throws Exception {
        this.pricelistSearch();
    }

    public void onClick$toolbarbuttonPricelistRefresh(Event event) throws Exception {
        this.pricelistRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.pricelistEdit();
    }
    
    private void pricelistCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/pricelist.zul");
    }

    private void pricelistEdit() throws Exception {
        if (this.selectedPricelist != null) {
            this.execution.sendRedirect("~./zul/html/pricelist.zul?Id=" + this.selectedPricelist.getId().toString());
        }
    }

    private void pricelistDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxPricelist.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    Pricelist pricelist = (Pricelist) selectedItem.getValue();
                    this.pricelistService.remove(pricelist);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.pricelists = this.pricelistService.search(this.searchPricelist);
        this.binder.loadAll();
    }

    private void pricelistSearch() throws Exception {
        this.execution.setAttribute("searchPricelist", this.searchPricelist);

        Window window = (Window) this.execution.createComponents("~./zul/html/pricelistSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchPricelist = (Search) this.execution.getAttribute("searchPricelist");
        }

        this.pricelists = this.pricelistService.search(this.searchPricelist);
        this.binder.loadAll();
    }

    private void pricelistRefresh() throws Exception {
        this.pricelists = this.pricelistService.search(this.searchPricelist);
        this.binder.loadAll();
    }
}
